package com.zerdoor.controller;

import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.RbacManagerService;
import com.zerdoor.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2015/2/2.
 * 后台管理员
 */
@Controller
@RequestMapping("rbacManagers")
public class RbacManagersController {
    @Autowired
    private RbacManagerService rbacManagerService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(Model model){
        model.addAttribute("roles", rbacManagerService.getRbacRoles());
        return "/rbac/managers";
    }

    @RequestMapping(value = "/view",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> view(HttpServletRequest request,int page,int rows){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        params.put("title", request.getParameter("title"));
        List<Map<String, Object>> list = rbacManagerService.getRbacManagers(params);
        PageInfo pageInfo = new PageInfo(list);
        result.put("rows",list);
        result.put("total",pageInfo.getTotal());
        return result;
    }

    /**
     * 添加标签风格
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> add(HttpServletRequest request){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        String psd = request.getParameter("psd");
        String repeatPsd = request.getParameter("repeatPsd");
        //密码验证
        if("".equals(psd)){
            result.put("errormsg","密码不能为空");
            return result;
        }
        if(!psd.equals(repeatPsd)){
            result.put("errormsg","2次输入密码不一样，请重新输入");
            return result;
        }
        //账号唯一性验证
        String loginName = request.getParameter("loginName");
        if("".equals(loginName)){
            result.put("errormsg","登陆账号不能为空");
            return result;
        }
        if(!rbacManagerService.getTotalByLoginName(loginName)){
            result.put("errormsg","管理员登陆账号已经存在");
            return result;
        }
        params.put("userId",null);
        params.put("orgId",1);
        params.put("roleId",Integer.parseInt(request.getParameter("roleId")));
        params.put("userName",request.getParameter("userName"));
        params.put("psd", MD5.getMd5String(request.getParameter("psd")));
        params.put("loginName",loginName);
        params.put("isValid",Integer.parseInt(request.getParameter("isValid")));
        params.put("phone",request.getParameter("phone"));
        params.put("email",request.getParameter("email"));
        params.put("encryptionkey", "");

        try {
            rbacManagerService.addRbacManagers(params);
        } catch (Exception e) {
            result.put("errormsg","添加失败");
        }
        return result;
    }

    /**
     * 修改管理者
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> edit(HttpServletRequest request,int id){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId",Integer.parseInt(request.getParameter("id")));
        params.put("roleId",Integer.parseInt(request.getParameter("roleId")));
        params.put("userName",request.getParameter("userName"));
        params.put("isValid",Integer.parseInt(request.getParameter("isValid")));
        params.put("phone",request.getParameter("phone"));
        params.put("email",request.getParameter("email"));
        try {
            rbacManagerService.updateRbacManagers(params);
        } catch (Exception e) {
            result.put("errormsg", "修改失败");
        }
        return result;
    }

    /**
     * 删除管理者
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            rbacManagerService.deleteRbacManagers(id);
        } catch (Exception e) {
            result.put("errormsg", "删除失败");
        }
        return result;
    }


    @RequestMapping(value = "/activate",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> activate(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("userId",id);
        try {
            rbacManagerService.updateActivationRbacManagers(result);
        } catch (Exception e) {
            result.put("errormsg","激活失败");
        }
        return result;
    }

    @RequestMapping(value = "/stop",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> stop(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("userId",id);
        try {
            rbacManagerService.updateStopRbacManagers(result);
        } catch (Exception e) {
            result.put("errormsg","停用失败");
        }
        return result;
    }
}
